package com.threeonemeeting.tab;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.mobclick.android.MobclickAgent;
import com.threeonemeeting.R;
import com.threeonemeeting.util.MiscUtil;

// Tab中的Activity的基类，所有内嵌的Activity都从 BaseTabActivity继承
public class BaseTabActivity extends ActivityGroup {

	protected String strFromActivity; // 当前Activity从哪个Activity跳转而来

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = this.getIntent();
		if (intent == null)
			return;

		Bundle b = intent.getExtras();
		if (b == null)
			return;

		strFromActivity = b.getString("from");

		// 返回
		Button btnBack = (Button) findViewById(R.id.back);
		if (btnBack != null) {
			btnBack.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					back();
				}
			});
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	public void init(String strTitle) {
		MiscUtil.setBackBtnListener(this);
		MiscUtil.setTitle(this, strTitle);
	}

	public MultiActivityTab getParentTab() {
		MultiActivityTab tabActivity = (MultiActivityTab) this.getParent();
		return tabActivity;
	}

	public void startActivity(String strToActivity) {
		if (strToActivity == null)
			return;

		((MultiActivityTab) getParent()).setCurrentTab(strToActivity);
	}

	public void startActivity(String strToActivity, Bundle b) {
		if (strToActivity == null || b == null)
			return;

		((MultiActivityTab) getParent()).setCurrentTab(strToActivity, b);
	}

	public void startActivity(String strFromActivity, String strToActivity) {
		if (strToActivity == null)
			return;

		MultiActivityTab parentTab = (MultiActivityTab) getParent();
		if (parentTab != null) {
			parentTab.setCurrentTab(strFromActivity, strToActivity);
		}
	}

	public void startActivity(String strFromActivity, String strToActivity,
			Bundle b) {
		if (strToActivity == null || b == null)
			return;

		((MultiActivityTab) getParent()).setCurrentTab(strFromActivity,
				strToActivity, b);
	}

	public boolean canBack() {

		if (strFromActivity == null
				|| strFromActivity.trim().equals("") == true)
			return false;

		return true;
	}

	public void back() {
		if (strFromActivity == null)
			return;

		((MultiActivityTab) BaseTabActivity.this.getParent())
				.backToTab(strFromActivity);
	}

	/**
	 * @desc 对界面进行重新加载
	 */
	public void refresh() {

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		Activity parent = getParent();
		if (parent != null) {
			return parent.onKeyDown(keyCode, event);
		}

		return super.onKeyDown(keyCode, event);
	}
}
